<template>
    <div>
        <el-row>
            <el-col>
                <el-form :inline="true" :model="formInline">
                    <el-form-item>
                        <el-input size="mini" v-model="formInline.enumName" placeholder="请输入名称"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-select clearable size="mini" v-model="formInline.enumTypeNumber" placeholder="请选择题型">
                            <el-option v-for="list in options" :label="list.ENUMTYPENAME" :value="list.ENUMTYPENUMBER"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-select clearable size="mini" v-model="formInline.status" placeholder="请选择状态">
                            <el-option label="启用" value="1"></el-option>
                            <el-option label="禁用" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="mini" @click="search">查询</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row class="buttonList" style="margin-top: 5px">
            <el-col>
                <el-button type="primary" size="mini" @click="newAddData">新增</el-button>
                <el-button :disabled="btnDisabled" type="primary" size="mini" @click="edit">编辑</el-button>
                <el-button :disabled="editDisabled" type="danger" size="mini" @click="synchronization">启用/禁用</el-button>
            </el-col>
        </el-row>
        <el-row>
            <el-col>
                <el-table
                        :data="data"
                        border
                        size="mini"
                        @selection-change="handleSelectionChange"
                        style="width: 100%">
                    <el-table-column
                            align="center"
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="ENUMNAME"
                            label="名称">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="ENUMNUMBER"
                            label="编号">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="ENUMTYPENAME"
                            label="类型">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="STATUS"
                            label="状态">
                        <template slot-scope="scope">
                            {{scope.row.STATUS ? '启用' : '禁用'}}
                        </template>
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="CREATEUSERNAME"
                            label="创建人">
                    </el-table-column>
                    <el-table-column
                            width="160px"
                            align="center"
                            prop="CREATETIME"
                            label="创建时间">
                        <template slot-scope="scope">
                            {{new Date(scope.row.CREATETIME).toLocaleString()}}
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        background
                        layout="total, sizes, prev, pager, next, jumper"
                        :page-size="pagination.pageSize"
                        :total="pagination.total"
                        :page-sizes="[10, 20, 30, 50, 100]"
                >
                </el-pagination>
            </el-col>
        </el-row>
        <el-dialog title="设备属性" :visible.sync="dialogFormVisible" center width="50%">
            <el-form :model="form" ref="form" :label-width="formLabelWidth">
                <el-row>
                    <el-col :span="12">
                        <el-form-item
                                prop="enumName"
                                :rules="{ required: true, message: '请输入名称', trigger: 'blur' }"
                                label="名称">
                            <el-input size="mini" v-model="form.enumName" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                                prop="enumNumber"
                                :rules="{ required: true, message: '请输入编号', trigger: 'blur' }"
                                label="编号">
                            <el-input size="mini" v-model="form.enumNumber" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                                prop="enumTypeNumber"
                                :rules="{ required: true, message: '请选择类型', trigger: 'change' }"
                                label="类型">
                            <el-select clearable size="mini" @change="changeType" v-model="form.enumTypeNumber" placeholder="请选择类型">
                                <el-option v-for="list in options" :label="list.ENUMTYPENAME" :value="list.ENUMTYPENUMBER"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
                <el-button size="mini" type="primary" @click="newAdd">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "baseData",
        data() {
            return {
                formInline: {},
                pagination: {
                    pageSize: 20,
                    total: 1,
                    pageNo: 1
                },
                dialogFormVisible: false,
                data: [],
                form: {
                    enumName: '',
                    enumNumber: '',
                    enumTypeName: '',
                    enumTypeNumber: ''
                },
                formLabelWidth: '80px',
                selectId: [],
                btnDisabled: true,
                editDisabled: true,
                isEdit: '',
                options: []
            }
        },
        created() {
            this.getData(this.pagination.pageSize, this.pagination.pageNo);
            this.$post('/public/findEnumTypeNumberAll').then(res => {
                this.options = res.data.data.dataList;
            })
        },
        methods: {
            search() {
                let obj = Object.assign({}, this.formInline, this.pagination);
                this.$post('/enum/findEnum', obj).then(res => {
                    this.data = res.data.data.dataList;
                    this.pagination.total = res.data.data.total;
                })
            },
            getData(pageSize, pageNo) {
                this.$post('/enum/findEnum', {pageSize, pageNo}).then(res => {
                    this.data = res.data.data.dataList;
                    this.pagination.total = res.data.data.total;
                })
            },
            newAddData() {
                this.dialogFormVisible = true;
                this.isEdit = '';
            },
            newAdd() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        let url = '';
                        if(this.isEdit === 'edit') {
                            url = '/enum/update';
                            this.form.id = this.selectId[0].ID;
                        } else {
                            url = '/enum/add';
                        }
                        this.$post(url, this.form).then(res => {
                            if(res.data.status === 1) {
                                this.dialogFormVisible = false;
                                this.getData(this.pagination.pageSize, this.pagination.pageNo);
                                this.$message.success('保存成功');
                            } else {
                                this.$message.error('保存失败,请稍后再试');
                            }
                        });
                    }
                })
            },
            edit() {
                this.dialogFormVisible = true;
                this.isEdit = 'edit';
                this.form.enumName = this.selectId[0].ENUMNAME;
                this.form.enumNumber = this.selectId[0].ENUMNUMBER;
                this.form.enumTypeNumber = this.selectId[0].ENUMTYPENUMBER;
            },
            changeType(val) {
                this.form.enumTypeName = (this.options.filter(item => item.ENUMTYPENUMBER === val))[0].ENUMTYPENAME;
            },
            synchronization() {
                let arr = [];
                for(let i = 0; i < this.selectId.length; i++) {
                    arr.push(this.selectId[i].ID);
                }
                this.$post('/insrtumentClassification/inventory', {ids: arr.join()}).then(res => {
                    if(res.data.status === 1) {
                        this.$message.success('同步成功');
                    } else {
                        this.$message.error('同步失败，请稍后再试');
                    }
                })
            },
            handleSelectionChange(val) {
                this.selectId = val;
                if(val.length === 1) {
                    this.btnDisabled = false;
                } else {
                    this.btnDisabled = true;
                }
                if(val.length > 0) {
                    this.editDisabled = false;
                } else {
                    this.editDisabled = true;
                }
            },
            handleSizeChange(size) {
                this.pagination.pageSize = size;
                this.getUser(size, this.pagination.pageNo);
            },
            handleCurrentChange(page) {
                this.pagination.pageNo = page;
                this.getUser(this.pagination.pageSize, page);
            }
        }
    }
</script>

<style scoped lang="scss">
    /deep/.el-form-item__error {
        top: 32px;
    }
</style>
